package com.barry.oscheckpoint;

import java.text.DecimalFormat;

import uk.me.jstott.jcoord.LatLng;
import uk.me.jstott.jcoord.OSRef;
import android.util.Log;

public class LonLatOS {
	
/*
 * convert OS grid reference to geodesic co-ordinates
 */
public static double[] OSGridToLatLong(String gridRef) {
		 LatLng ll1w = null;
	    String[] gr = gridrefLetToNum(gridRef);
	    int E = Integer.parseInt(gr[0]); 
	    int N = Integer.parseInt(gr[1]);
	    try{
		    OSRef os1w = new uk.me.jstott.jcoord.OSRef(E, N);
		    ll1w = os1w.toLatLng();
		    ll1w.toWGS84();
		    return new double[]{ll1w.getLat(),ll1w.getLng()};
	    }
	    catch(Exception ex){
	    	 Log.d("tag", ex.toString());

	    }
		return null;
}


/* 
 * convert standard grid reference ('SU387148') to fully numeric ref ([438700,114800])
 *   returned co-ordinates are in metres, centred on grid square for conversion to lat/long
 *
 *   note that northern-most grid squares will give 7-digit northings
 *   no error-checking is done on gridref (bad input will give bad results or NaN)
 */
public static String[] gridrefLetToNum(String gridref) {
  // get numeric values of letter references, mapping A->0, B->1, C->2, etc:
  int l1 = Character.codePointAt(gridref, 0) - Character.codePointAt("A", 0);
  int l2 = Character.codePointAt(gridref, 1) - Character.codePointAt("A", 0);
  // shuffle down letters after 'I' since 'I' is not used in grid:
  if (l1 > 7) l1--;
  if (l2 > 7) l2--;

  // convert grid letters into 100km-square indexes from false origin (grid square SV):
  int eInt = (((l1-2)%5)*5 + (l2%5));
  String e = new DecimalFormat("#").format(eInt);
  double nDbl = (19-Math.floor(l1/5)*5) - Math.floor(l2/5);
  String n = new DecimalFormat("#").format(nDbl);
  // skip grid letters to get numeric part of ref, stripping any spaces:
  gridref = gridref.substring(2).replaceAll("[ \t]","");

  // append numeric part of references to grid index:
  e += gridref.substring(0, gridref.length()/2);
  n += gridref.substring(gridref.length()/2);

  // normalise to 1m grid, rounding up to centre of grid square:
  switch (gridref.length()) {
    case 6: 
    	e += "50"; 
        n += "50"; 
        break;
    case 8: 
    	e += "5"; 
    	n += "5";
       break;
    // 10-digit refs are already 1m
  }

  return new String[] {e, n};
}
	
	
	public static String fromCharCode(int... codePoints) {
	    return new String(codePoints, 0, codePoints.length);
	}
	
	/*
	 * extend Number object with methods for converting degrees/radians
	 */
	private static double toRad(double aNo) {  // convert degrees to radians
	  return aNo * Math.PI / 180;
	}

	 /*
	  * var x = (lon2-lon1) * Math.cos((lat1+lat2)/2);
		var y = (lat2-lat1);
		var d = Math.sqrt(x*x + y*y) * R;
	  */
	//   (-2.053926, -2.058483 , 53.384126, 53.384126)
	 public static double DistanceToCheckPoint(double cpLon, double  cpLat, double curLon, double curLat){
		 int R = 6371;
		 double d;
		 double x = (toRad(cpLon)-toRad(curLon)) * Math.cos((toRad(curLat)+toRad(cpLat))/2);
		 double y = (toRad(cpLat)-toRad(curLat));
		 d = Math.sqrt(x*x + y*y) * R;
		 return d;
	 }
	 
	 public static double TestPythagoras(){
		 int R = 6371;
		 double d;
		 double x = (toRad(-2.053926)-toRad(-2.058483)) * Math.cos((toRad(53.384126)+toRad(53.3795290))/2);
		 double y = (toRad(53.379529)-toRad(53.384126));
		 d = Math.sqrt(x*x + y*y) * R;
		 
		 double xs = (toRad(-2.033855)-toRad(-2.058483)) * Math.cos((toRad(53.384126)+toRad(53.375058))/2);
		 double ys = (toRad(53.375058)-toRad(53.384126));
		 d = Math.sqrt(xs*xs + ys*ys) * R;	 
		 
		 double xh = (toRad(-1.946146)-toRad(-2.058483)) * Math.cos((toRad(53.384126)+toRad(53.378270))/2);
		 double yh = (toRad(53.378270)-toRad(53.384126));
		 d = Math.sqrt(xh*xh + yh*yh) * R;			 
	    	return d;
	    }
	 
	
	
}